<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="12">
          <a-col :md="4" :sm="8">
            <a-form-item label="车牌号">
              <a-input placeholder="请输入车牌号" v-model="queryParam.plateNo"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="8">
            <a-form-item label="车辆类型">
              <a-select placeholder="请选择车辆类型" v-model="queryParam.carNature" :options="carNatureOption"></a-select>
            </a-form-item>
          </a-col>


          <a-col :md="4" :sm="8">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
            </span>
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="resetQuery" icon="search">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" icon="download" @click="handleExportXls('场内滞留车辆')">导出</a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchOut">
            <a-icon type="delete"/>
            出场
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作
          <a-icon type="down"/>
        </a-button>
      </a-dropdown>
    </div>
    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择<a
          style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>
      <a-table
          ref="table"
          size="middle"
          bordered
          rowKey="id"
          :columns="columns"
          :dataSource="dataSource"
          :pagination="ipagination"
          :loading="loading"
          :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
          @change="handleTableChange"
          :scroll="{ x: 1400}"
          style="word-break: break-all"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="handleOut(record)">手动出场</a>
          <a-divider type="vertical"/>
          <a-popconfirm
              title="确定设置吗?"
              @confirm="() => handleBlack(record.id)"
          >
            <a>设置黑名单</a>
          </a-popconfirm>
          <a-divider type="vertical"/>
          <a @click="handleDetail(record)">详情</a>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->
    <!-- 表单区域 -->
    <zombieCar-modal ref="modalForm" @ok="modalFormOk"></zombieCar-modal>
    <zombieCar-appeared ref="appearedForm" @ok="appearedFormOk"></zombieCar-appeared>
    <zombieCar-outBatch ref="outBatchForm" @ok="appearedFormOk"></zombieCar-outBatch>
  </a-card>
</template>

<script>
import zombieCarModal from './modules/zombieCarModal'
import zombieCarAppeared from './modules/zombieCarAppeared.vue'
import zombieCarOutBatch from './modules/zombieCarOutBatch.vue'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import {getAction, downFile} from '@/api/manage'
import {filterObj} from '@/utils/util'

export default {
  name: 'ZombieCarList',
  mixins: [JeecgListMixin],
  components: {
    zombieCarModal,
    zombieCarAppeared,
    zombieCarOutBatch,
  },
  data() {
    return {
      description: '滞留车信息管理页面',
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          align: 'center',
          width: 60,
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '停车场名称',
          align: 'center',
          dataIndex: 'parkName',
        },
        {
          title: '车牌号',
          align: 'center',
          dataIndex: 'plateNo',
        },
        {
          title: '滞留天数',
          align: 'center',
          dataIndex: 'stayDay',
        },
        {
          title: '车辆类型',
          align: 'center',
          dataIndex: 'carType',
          width: 100,
          customRender: (v) => ('0' === v ? '无牌车' : '有牌车'),
        },
        {
          title: '车辆性质',
          align: 'center',
          dataIndex: 'carNature',
          width: 100,
          customRender: (v) => {
            if (v === '0' || v === 0) {
              return '临时车'
            } else if (v === '1' || v === 1) {
              return '月租车'
            } else if (v === '2' || v === 2) {
              return '储值车'
            } else if (v === '3' || v === 3) {
              return '军警车'
            } else if (v === '4' || v === 4) {
              return '白名单'
            }
          },
        },
        {
          title: '进场时间',
          align: 'center',
          dataIndex: 'carEnterTime',
          width: 200,
        },
        {
          title: '车辆进场地点',
          align: 'center',
          dataIndex: 'armName',
        },
        {
          title: '抓拍图片',
          align: 'center',
          dataIndex: 'imgPath',
          customRender: (record, row, index) => {
            let r = record
            return (
                <img
                    style="max-width:55px;max-height:55px;min-height:20px;min-width:20px;text-align:center;cursor:pointer;border:1px #BFBFBF solid;padding: 2px 2px 2px 2px;background-color:white;"
                    preview={r}
                    src={record}
                />
            )
          },
        },
        {
          title: '创建时间',
          align: 'center',
          dataIndex: 'createTime',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          width: 250,
          scopedSlots: {customRender: 'action'},
        },
      ],
      url: {
        list: '/parking/zombieCar/list',
        out: '/parking/zombieCar/out',
        outBatch: '/parking/zombieCar/outBatch',
        black: '/parking/zombieCar/black',
        exportXlsUrl: 'parking/zombieCar/exportXls',
        importExcelUrl: 'parking/zombieCar/importExcel',
        loadParkCode: '/parking/parkInfo/parkInfoList',
      },
      queryParam: {
        carNature:'',
        plateNo:'',
        parkCode: '',
        type: '-1',
      },
      isorter: {
        column: 'createTime',
        order: 'desc',
      },
      parkCodeList: [],
      carNatureOption: [{value: '',label:'请选择车辆类型'},{value: '0', label: '临时车'}, {value: '1', label: '月租车'}, {value: '2', label: '储值车'}, {
        value: '3',
        label: '军警车'
      }, {value: '4', label: '白名单'}]
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  created() {
    this.keyupEnter()
  },
  mounted() {
    this.loadData()
  },
  methods: {
    handleExportXls(fileName) {
      let param = {...this.queryParam}

      downFile(this.url.exportXlsUrl, param).then((data) => {
        if (!data) {
          this.$message.error('文件下载失败')
          return
        }
        if (data.size === 0) {
          this.$message.warning('当前滞留车辆列表为空，请修改查询条件后重试！')
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data]), fileName + '.xls')
        } else {
          let url = window.URL.createObjectURL(new Blob([data]))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName + '.xls')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link)
          window.URL.revokeObjectURL(url)
        }
      })
    },
    async loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      if (arg === 1) {
        this.ipagination.current = 1
      }
      await this.getParkCodeList()
      var params = this.getQueryParams()
      this.loading = true
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    filterOption(input, option) {
      let com = option.componentOptions
      let parkCode = String(com.propsData.value)
      let parkName = String(com.children[0].text)
      if (parkCode.toLowerCase().includes(input.toLowerCase()) || parkName.toLowerCase().includes(input.toLowerCase())) {
        return true
      }
    },
    searchQuery() {
      this.loadData(1)
    },
    resetQuery() {
      this.queryParam.plateNo = ''
      this.queryParam.carNature = this.carNatureOption[0].value
      this.loadData()
    },
    async getParkCodeList() {
      const that = this
      if ('' !== that.queryParam.parkCode) {
        return
      }
      await getAction(this.url.loadParkCode, {}).then(res => {
        if (res.success) {
          that.parkCodeList = []
          res.result.forEach(res => {
            that.parkCodeList.push({
              value: res.parkCode,
              label: res.parkName
            })
          })
          if (res.result.length > 0) {
            that.queryParam.parkCode = that.parkCodeList[0].value
          }
        }
      })
    },

    getQueryParams() {
      var param = Object.assign({}, this.isorter, this.queryParam)
      param.field = this.getQueryField()
      param.pageNo = this.ipagination.current
      param.pageSize = this.ipagination.pageSize
      return filterObj(param)
    },

    handleDetail: function (record) {
      this.$refs.modalForm.title = '详情'
      this.$refs.modalForm.edit(record)
    },
    appearedFormOk() {
      this.loadData(1)
      this.onClearSelected();
    },
    handleOut: function (record) {
      this.$refs.appearedForm.title = '请选择车道信息'
      this.$refs.appearedForm.edit(record)
    },
    // 批量出场
    batchOut: function () {
      if (!this.url.outBatch) {
        this.$message.error('请设置url.outBatch属性!')
        return
      }
      if (this.selectionRows.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        var plateNos = ""
        for (var a = 0; a < this.selectionRows.length; a++) {
          plateNos += this.selectionRows[a].plateNo + ','
        }
        let params = {}
        params.parkCode = this.queryParam.parkCode
        params.plateNos = plateNos
        this.$refs.outBatchForm.title = '请选择车道信息'
        this.$refs.outBatchForm.batchEdit(params)
      }
    },
    handleBlack: function (id) {
      console.log(id)
      if (!this.url.black) {
        this.$message.error('请设置url.black属性!')
        return
      }
      var that = this
      getAction(that.url.black, {id: id}).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.loadData()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    keyupEnter() {
      document.onkeydown = (e) => {
        let body = document.getElementsByTagName('body')[0]
        if (e.keyCode === 13) {
          this.loadData(1)
        }
      }
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
